package com.oa.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.joneying.common.annotation.CommonController;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.LeaveApplyAddDTO;
import com.oa.admin.dto.LeaveApplyListDTO;
import com.oa.admin.entity.LeaveApply;
import com.oa.admin.service.ILeaveApplyService;
import com.oa.admin.vo.LeaveApplyDetailsVO;
import com.oa.admin.vo.LeaveApplyListVO;
import com.oa.core.BaseController;
import com.oa.core.PageResult;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.system.entity.ApplyApproval;
import com.oa.system.service.IApplyApprovalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "请假申请")
@RequestMapping("/leaveApply")
@RestController
public class LeaveApplyController extends BaseController {

    @Autowired
    private ILeaveApplyService leaveApplyService;

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @GetMapping("/list")
    @CommonController
    @Description("列表")
    @ApiOperation(value = "列表")
    public Resp<PageResult<LeaveApplyListVO>> findLeaveApplyList(LeaveApplyListDTO leaveApplyListDTO){
        leaveApplyListDTO.setUserId(getLoginUserId());
        return new Resp().success(leaveApplyService.findLeaveApplyList(leaveApplyListDTO));
    }

    @GetMapping("/{id}")
    @CommonController
    @Description("详情")
    @ApiOperation(value = "详情")
    public Resp<LeaveApplyDetailsVO> findLeaveApplyDetails(@PathVariable String id){
        return new Resp().success(leaveApplyService.findLeaveApplyDetails(id));
    }

    @PostMapping("")
    @CommonController
    @Description("添加申请")
    @ApiOperation(value = "添加申请")
    public Resp add(@RequestBody LeaveApplyAddDTO leaveApplyAddDTO) throws BusinessException, DataIsEmptyException {

        if(leaveApplyAddDTO.getLeaveEndTime().getTime() <= leaveApplyAddDTO.getLeaveStartTime().getTime()){
            return new Resp().failure("结束时间不能小于开始时间");
        }

        List<LeaveApply> leaveApplys = leaveApplyService.selectList(
                new EntityWrapper().where("NOT ((leave_end_time <= {0})  OR (leave_start_time >= {1})) and user_id = {2}", leaveApplyAddDTO.getLeaveStartTime(), leaveApplyAddDTO.getLeaveEndTime() ,getLoginUserId()));

        for(LeaveApply leaveApply : leaveApplys){
            ApplyApproval applyApproval = applyApprovalService.selectOne(new EntityWrapper().eq("apply_id", leaveApply.getId()));
            if(applyApproval != null){
                if(!applyApproval.getApprovalNodeId().equals(0)){
                    return new Resp().failure("该时间段已存在申请");
                }
            }
        }
        return leaveApplyService.add(getLoginUserId(), leaveApplyAddDTO);
    }

    @PutMapping("/audit")
    @CommonController
    @Description("审批")
    @ApiOperation(value = "审批")
    public Resp audit(
            @ApiParam("请假申请ID") @RequestParam String id,
            @ApiParam("申请表ID") @RequestParam String applyApprovalId,
            @ApiParam("状态") @RequestParam Integer status) throws BusinessException, DataIsEmptyException {

        return leaveApplyService.audit(id, applyApprovalId, status);
    }

    @GetMapping("/export")
    @CommonController
    @Description("导出")
    @ApiOperation(value = "导出")
    public void export(LeaveApplyListDTO leaveApplyListDTO){
        leaveApplyListDTO.setUserId(getLoginUserId());
        leaveApplyService.export(leaveApplyListDTO, response);
    }

    @PutMapping("/save/file")
    @CommonController
    @Description("导出")
    @ApiOperation(value = "导出")
    public Resp saveFile(@RequestParam String fileName, @RequestParam String fileAddress, @RequestParam String id){
        LeaveApply leaveApply = new LeaveApply();
        leaveApply.setId(id);
        leaveApply.setLeaveFileName(fileName);
        leaveApply.setLeaveFileAddress(fileAddress);
        if(leaveApplyService.updateById(leaveApply)){
            return new Resp().success();
        }
        return new Resp().failure();
    }
}
